package com.lzx.iteam.net;

import android.os.Message;
import com.lzx.iteam.bean.CRMBaseBean;
import com.lzx.iteam.bean.CRMChanceBean;
import com.lzx.iteam.bean.CRMCommentData;
import com.lzx.iteam.bean.CRMLibraryListData;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CRMDetailMsg extends CcMsgStructure {

    private int type;
    private Message mCallBack;

    public CRMDetailMsg(int type , Message callback) {
        this.mCallBack = callback;
        this.type = type;
    }

    @Override
    public void onReceive(JSONObject data) {
        Map<String, Object> map = new HashMap<String, Object>();
        CRMBaseBean detail = new CRMBaseBean();
        CRMChanceBean chance = new CRMChanceBean();
        ArrayList<CRMCommentData> commentDatas = new ArrayList<CRMCommentData>();

        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);

            JSONObject detailInfo = jsonObject.getJSONObject("info");
            if (type == Constants.CRM_PUBLIC_CLUE){
                detail.setStatus(detailInfo.getInt("status"));
                detail.setName(detailInfo.getString("name"));
                detail.setPhone(detailInfo.getString("phone"));

                if (detailInfo.has("sex")){
                    detail.setSex(detailInfo.getInt("sex"));
                }
                if (detailInfo.has("company")){
                    detail.setCompany(detailInfo.getString("company"));
                }
                if (detailInfo.has("department")){
                    detail.setDepartment(detailInfo.getString("department"));
                }
                if (detailInfo.has("job")){
                    detail.setJob(detailInfo.getString("job"));
                }
                if (detailInfo.has("telephone")) {
                    detail.setTelephone(detailInfo.getString("telephone"));
                }
                if (detailInfo.has("email")) {
                    detail.setEmail(detailInfo.getString("email"));
                }
                if (detailInfo.has("address")){
                    detail.setAddress(detailInfo.getString("address"));
                }
                if (detailInfo.has("latitude")) {
                    detail.setLatitude(detailInfo.getString("latitude"));
                }
                if (detailInfo.has("longitude")) {
                    detail.setLongitude(detailInfo.getString("longitude"));
                }
                if (detailInfo.has("code")){
                    detail.setCode(detailInfo.getString("code"));
                }
                if (detailInfo.has("origin")){
                    detail.setOrigin(detailInfo.getInt("origin"));
                }
                if (detailInfo.has("active_time")) {
                    detail.setActiveTime(detailInfo.getString("active_time"));
                }
                if (detailInfo.has("owner")) {
                    detail.setOwner(detailInfo.getString("owner"));
                }
                if (detailInfo.has("owner_name")) {
                    detail.setOwnerName(detailInfo.getString("owner_name"));
                }
                if (detailInfo.has("owner_img")) {
                    detail.setOwnerImg(detailInfo.getString("owner_img"));
                }
                if (detailInfo.has("remark")){
                    detail.setRemark(detailInfo.getString("remark"));
                }
            }else if (type == Constants.CRM_CHANCE){
                chance.setOwner_name(detailInfo.getString("owner_name"));
                chance.setOwner(detailInfo.getString("owner"));
                chance.setOwner_img(detailInfo.getString("owner_img"));
                JSONArray clues = detailInfo.getJSONArray("clues");
                ArrayList<CRMLibraryListData> crmClues = new ArrayList<CRMLibraryListData>();
                for (int i = 0; i < clues.length(); i++) {
                    JSONObject obj = clues.getJSONObject(i);
                    CRMLibraryListData clue = new CRMLibraryListData();
                    clue.setName(obj.getString("name"));
                    clue.setPhone(obj.getString("phone"));
                    clue.set_id(obj.getString("_id"));
                    crmClues.add(clue);
                }
                chance.setClues(crmClues);
                if (detailInfo.has("chance_type")){
                    chance.setChance_type(detailInfo.getInt("chance_type"));
                }
                if (detailInfo.has("chance_name")){
                    chance.setChance_name(detailInfo.getString("chance_name"));
                }
                if (detailInfo.has("market_amount")){
                    chance.setMarket_amount(detailInfo.getString("market_amount"));
                }
                if (detailInfo.has("market_stage")){
                    chance.setMarket_stage(detailInfo.getInt("market_stage"));
                }
                if (detailInfo.has("update_time")){
                    chance.setUpdate_time(detailInfo.getString("update_time"));
                }
                if (detailInfo.has("statement_date")){
                    chance.setStatement_date(detailInfo.getString("statement_date"));
                }
                if (detailInfo.has("chance_source")){
                    chance.setChance_source(detailInfo.getInt("chance_source"));
                }
                if (detailInfo.has("active_time")){
                    chance.setActive_time(detailInfo.getString("active_time"));
                }
                if (detailInfo.has("remark")){
                    chance.setRemark(detailInfo.getString("remark"));
                }

            }

            JSONObject detailHistory = jsonObject.getJSONObject("history");
            if (detailHistory.has("history")) {
                JSONArray history = detailHistory.getJSONArray("history");
                for (int i = 0; i < history.length(); i++) {
                    JSONObject item = history.getJSONObject(i);
                    CRMCommentData commentData = new CRMCommentData();
                    commentData.setContent(item.getString("content"));
                    commentData.setType(item.getInt("type"));
                    commentData.setcTime(item.getString("ctime"));
                    commentData.setUserId(item.getString("uid"));
                    commentData.setUserName(item.getString("uname"));
                    commentData.setUserImage(item.getString("image"));
                    commentData.setFlag(item.getInt("flag"));
                    if (item.has("attachment")){
                        commentData.setAttachments(item.getJSONArray("attachment").toString());
                    }
                    commentDatas.add(commentData);
                }
            }
            if (type == Constants.CRM_PUBLIC_CLUE){
                map.put("info", detail);
            }else if (type == Constants.CRM_CHANCE){
                map.put("info", chance);
            }
            Collections.reverse(commentDatas);
            map.put("history", commentDatas);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (mCallBack != null) {
            mCallBack.arg1 = 0;
            mCallBack.obj = map;
            mCallBack.sendToTarget();
        }
    }

    @Override
    public void onError(int error, Object ret) {
        String errorMsg = null;
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                errorMsg = jData.getString("msg");
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mCallBack != null) {
            mCallBack.arg1 = error;
            if (!StringUtil.isEmpty(errorMsg)) {
                mCallBack.obj = errorMsg;
            } else {
                mCallBack.obj = Constants.NETWORK_WRROR;
            }
            mCallBack.sendToTarget();
        }
    }
}
